package com.quectel.cms.controller.grid;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.grid.dto.GridMemberWorkLogDto;
import com.quectel.core.module.grid.service.GridMemberWorkLogService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/02/03 16:42
 */
@RestController
@RequestMapping("gridMemberWorkLog")
@Tag(name = "GridMemberWorkLogController", description = "网格员工作日志相关api")
public class GridMemberWorkLogController extends BaseController {

    @DubboReference
    private GridMemberWorkLogService gridMemberWorkLogService;

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "gridMemberWorkLog:list")
    @Operation(summary = "网格员工作日志的分页查询列表")
    public Response<Page<GridMemberWorkLogDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = gridMemberWorkLogService.queryTotal(request.getParams());

        List<GridMemberWorkLogDto> list = null;
        if (total > 0) {
            list = gridMemberWorkLogService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<GridMemberWorkLogDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<GridMemberWorkLogDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "gridMemberWorkLog:edit")
    @Operation(summary = "查询单条网格员工作日志记录")
    public Response<GridMemberWorkLogDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (gridMemberWorkLogService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        GridMemberWorkLogDto gridMemberWorkLogDto = gridMemberWorkLogService.selectById(id);

        return Response.<GridMemberWorkLogDto>ok().wrap(gridMemberWorkLogDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "gridMemberWorkLog:info")
    @Operation(summary = "查询单条网格员工作日志记录")
    public Response<GridMemberWorkLogDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (gridMemberWorkLogService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        GridMemberWorkLogDto gridMemberWorkLogDto = gridMemberWorkLogService.selectById(id);

        return Response.<GridMemberWorkLogDto>ok().wrap(gridMemberWorkLogDto);
    }

    /**
     * 保存
     *
     * @param gridMemberWorkLogDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "gridMemberWorkLog:edit")
    @Operation(summary = "保存单条网格员工作日志记录")
    public Response<Object> save(@RequestBody @Validated GridMemberWorkLogDto gridMemberWorkLogDto) {

        //填充基础数据
        fillSaveCommonData(gridMemberWorkLogDto);

        gridMemberWorkLogService.save(gridMemberWorkLogDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param gridMemberWorkLogDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "gridMemberWorkLog:edit")
    @Operation(summary = "修改单条网格员工作日志记录")
    public Response<Object> update(@RequestBody @Validated GridMemberWorkLogDto gridMemberWorkLogDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(gridMemberWorkLogDto.getId()));
        if (gridMemberWorkLogService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(gridMemberWorkLogDto);

        gridMemberWorkLogService.updateById(gridMemberWorkLogDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "gridMemberWorkLog:delete")
    @Operation(summary = "批量删除存在的网格员工作日志记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (gridMemberWorkLogService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        gridMemberWorkLogService.deleteBatch(ids);

        return Response.ok();
    }
}
